package com.example.esaclient;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

import com.google.gson.Gson;



public class GetServiceOn extends AsyncTask<Object, Integer, String[]> {
 
	private  String URL;
	private  static final String METHOD_NAME = "/GetServiceOn?";	
	TimelineActivity caller;
	
	public GetServiceOn(TimelineActivity callerActivity){
		caller=callerActivity;
	}
	
	@Override
	protected  String[] doInBackground(Object... params) {
	
	    	 try {
				  
				 GlobalSettings gs = new GlobalSettings(caller);
				 URL = "http://"+gs.getPrefString(GlobalSettings.PREF_ESA_URL)+"/ServiceRest.svc";			 
				 String url =URL+METHOD_NAME+"id="+params[0];
						  
				 HttpClient client = new DefaultHttpClient();
			        HttpGet httpGet = new HttpGet(url);
			        try {
			            HttpResponse response = client.execute(httpGet); 	
			            HttpEntity entity = response.getEntity();
			             
			            if(entity != null) {
			                InputStream stream = entity.getContent();
			                String responseString = GlobalSettings.convertStreamToString(stream);
			                
			                
			                   Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
			                   return w.GetServiceOnResult;
			                   
			            }
			             
			        } catch (ClientProtocolException e) {
			            e.printStackTrace();
			          
			        } catch (IOException e) {
			            e.printStackTrace();		            
			        }
		      }
		     catch (final Exception e) {
		         e.printStackTrace();   
		         
		     }
			
		        
	    
	return new String[0];
	}
	
	  public class Wrapper {
	    	 public String[] GetServiceOnResult;
	     }

	     protected void onPostExecute(String[] result) {
	    	 caller.GetServiceOnResult(result);
	     }

	
	

}
